package com.xjrsoft.module.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.module.app.dto.SetModuleDto;
import com.xjrsoft.module.app.entity.XjrAppFunction;
import com.xjrsoft.module.app.entity.XjrAppMyFunction;
import com.xjrsoft.module.app.service.IXjrAppFunctionService;
import com.xjrsoft.module.app.service.IXjrAppMyFunctionService;
import com.xjrsoft.module.app.vo.AppFunctionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * APP端功能模块管理表 前端控制器
 * </p>
 *
 * @author zwq
 * @since 2021-04-19
 */
@RestController
@RequestMapping("/app-my-function")
@AllArgsConstructor
@Api(value = "/app-my-function",tags = "app授权功能")
public class XjrAppMyFunctionController {

    @Autowired
    private IXjrAppFunctionService appFunctionService;

    @Autowired
    private IXjrAppMyFunctionService appMyFunctionService;


    @GetMapping("/my")
    @ApiOperation(value = "获取当前登录人的授权")
    public Response<List<AppFunctionVo>> getAppModule(){
        return Response.ok(appFunctionService.getAppModuleForCurrentUser());
    }

    @GetMapping
    @ApiOperation(value = "获取所有App功能")
    public Response<List<AppFunctionVo>> getAllAppModule(){
        return Response.ok(appFunctionService.getAllAppModule());
    }

    @GetMapping("/{type}")
    @ApiOperation(value = "获取所有App 功能 根据type分类")
    public Response<List<AppFunctionVo>> getAppModuleByType(@PathVariable String type){
        return Response.ok(appFunctionService.getAppModuleByType(type));
    }

    @PostMapping("/set-module")
    @ApiOperation(value = "授权App功能")
    public  Response<Boolean> setAppModule(@RequestBody SetModuleDto dto){
        return Response.ok(appMyFunctionService.setModule(dto));
    }

    @GetMapping("/role/{roleId}")
    @ApiOperation(value = "根据角色获取所授权app功能")
    public  Response< List<XjrAppMyFunction>> setAppModule(@PathVariable String roleId){
        List<XjrAppMyFunction> list = appMyFunctionService.list(Wrappers.<XjrAppMyFunction>query().lambda().select(XjrAppMyFunction::getFunctionId).eq(XjrAppMyFunction::getUserId, roleId));

        return Response.ok(list);
    }


}
